/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.meluk.billboard.jpa;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.security.core.userdetails.UserDetails;

/**
 *
 * @author AMelnyk
 */
@Entity
@Table(uniqueConstraints = {
    @UniqueConstraint(columnNames = {"name"}),
    @UniqueConstraint(columnNames = {"email"})
})
public class User implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String email;
    private String password;
    private String name;
    private Integer age;
    @Temporal(TemporalType.TIMESTAMP)
    private Calendar lastRequestTime;
    @OneToOne(mappedBy = "user")
    private Player player;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Calendar getLastRequestTime() {
        return lastRequestTime;
    }

    public void setLastRequestTime(Calendar lastRequestTime) {
        this.lastRequestTime = lastRequestTime;
    }

    public static User findByEmail(Session session, String email) {
        Query query = session.createQuery("from User u where u.email=:email");
        query.setString("email", email);
        List<User> userList = query.list();
        if (!userList.isEmpty()) {
            return userList.get(0);
        }
        return null;
    }

    public static User findByNameAndPassword(Session session, String name, String password) {
        Query query = session.createQuery("from User u where u.name=:name and u.password=:password");
        query.setString("name", name);
        query.setString("password", password);
        List<User> userList = query.list();
        if (!userList.isEmpty()) {
            return userList.get(0);
        }
        return null;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final User other = (User) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 97 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Player getPlayer() {
        return player;
    }

    public void setPlayer(Player player) {
        this.player = player;
    }
}
